if (typeof $.widget === 'function') {
	(function($) {
		$.widget("ui.yarncont", {
			options: {
				delay: 0,
				maxSize: 50,
				acbegin: 0,
				onpicked:'',
				onchanged:'',
				onsource:'',
				acwidth:0,
				validate:true
			},
			_create:function(){
				var self = this,
					el = self.element,
					maxSize =  self.options.maxSize,
					delay = self.options.delay,
					acwidth = self.options.acwidth,
					acbegin = self.options.acbegin,
					_onpicked = self.options.onpicked,
					_onchanged = self.options.onchanged,
					_onsource = self.options.onsource,
					_validate = self.options.validate,
					_name = $(el).attr('name'),//combox_hrnum
					_id = $(el).attr('id')//combox_hrnum_2
				var textel =  $(el);
				var re = new RegExp("[^a-zA-Z\-_\(\)\\.]{1,}", "gi"); 
				var reVal = new RegExp("[a-zA-Z\-_\(\)\\.]{1,}", "gi"); 
				
				// 设定默认值为<select>选定项目
					textel.autocomplete({
						autoFocusValue:false,
						source: function(request, response) {
							var term = $.trim(request.term);
							var ay = term.split(re);
							if(ay.length == 0)return;
							term = ay[ay.length-1];
							if(term.length == 0)return;
							
							var ay = new Array();
							if(term.length>=acbegin){
								for(var tempvalue in $P.autodata[_name]){
									if (tempvalue.toLowerCase().indexOf(term.toLowerCase()) == 0) {
										ay.push(tempvalue);
									}
									
									if(ay.length >= maxSize){
										break;
									}
								}
							}
							response(ay);
							delete ay;
							delete term;
						},
						width: acwidth,
						delay: 0,
						select: function(event, ui) {
							var tempvalue = textel.val();
							var ay1 = tempvalue.split(reVal);//比例
							if(ay1.length > 0){
								if(ay1[0].trim() == ""){
									ay1.shift();
								}
								
								if(ay1[ay1.length-1].trim() == ""){
									ay1.pop();
								}
							}
							var ay2 = tempvalue.split(re);//成份
							if(ay2.length > 0){
								if(ay2[0].trim() == ""){
									ay2.shift();
								}
								
								if(ay2[ay2.length-1].trim() == ""){
									ay2.pop();
								}
								ay2[ay2.length-1]=ui.item.value;
							}
							
							
							var text = "";
							var  pos = 0;
							for(var i = 0;i<ay1.length;i++){
								text += ay1[i]+ay2[i].toUpperCase();
							}
							textel.val(text);
							return false;
						},
						change: function(event, ui) {	// 用select事件来联动select,change只用来做输入不匹配时处理
							if((typeof _onchanged) == 'function'){
								_onchanged.call(this,ui.item);
							}
							
							if(_validate){
								var jo = $P.validYarncont(textel.val(),_name);
								if(jo.ret == 0){
									textel.val("");
									alert(jo.msg);
								}else{
									textel.val(jo.text);
								}
							}
						},
						minLength: acbegin
					});
				
				// FF下绑定input事件来兼容输入法中文输入
				if (jQuery.browser.mozilla) {
					textel[0].addEventListener(
						'input', 
						function() {
							var val = this.value;
							if (val) {
								$(this).autocomplete("search", val);
							}
						}, false
					);
				}
			}
		});
	})(jQuery);
}
// ui-autocomplete-input ui-widget ui-widget-content ui-corner-left ui-autocomplete-loading